﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RadioButtonListHelper.cs" company="ecblog.codeplex.com">
//   emrecoskun.net -  
// </copyright>
// <summary>
//   Defines the RadioButtonListHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EC.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;

    /// <summary>
    /// The radio button list helper
    /// </summary>
    public static class RadioButtonListHelper
    {
        /// <summary>
        /// The Radio button list for mvc view.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="items">The items.</param>
        /// <returns>The radio button list</returns>
        public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, params string[] items)
        {
            return htmlHelper.RadioButtonListFor(expression, new SelectList(items));
        }

        /// <summary>
        /// The Radio button list for mvc view.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="items">The items.</param>
        /// <returns>The radio button list</returns>
        public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items)
        {
            var func = expression.Compile();
            var result = func(htmlHelper.ViewData.Model);
            var list = new SelectList(items, "Value", "Text", result);
            return htmlHelper.EditorFor(expression, "RadioButtonList", new { selectList = list });
        }
    }
}